#/bin/bash

pathInToDo="/data/samba_share/##########/opl"
pathInDone=$pathInToDo"/done/"
logfile="/tmp/oplReport.sh.log"

function getData()
{
    # usage : $0 <opl path> <logistic center>
    path=$1
    lc=$2
  
    tmpfile="/tmp/tmpOplList"
    if [ -f $tmpfile ]; then
	`rm $tmpfile`
    fi
    
    path=`echo $path | sed s/##########/$lc/`
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Listing files in $path..."
    
    find $path -maxdepth 1 -name "*.000" -ctime 0 -exec head -1 {} \; | awk -F ";" '{ print $8" | "$5" | "$9" | "$27}' > $tmpfile
    
    filesize=`wc -l $tmpfile | awk '{ print $1 }'`
    
    if [ $filesize == 0 ]; then
	echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: No file found."
	return
    fi
    
    echo;cat $tmpfile;echo
}

lockfile="/tmp/oplreport.lock" 
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Checking lock file $lockfile..."
if [ -f $lockfile ]; then 
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Lock file exits! Aborting..." 
    exit
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Creating lock file..."
`touch $lockfile` 
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Processing... "

getData $pathInToDo tct-huizhou
getData $pathInDone tct-huizhou
getData $pathInToDo"_ns" tct-huizhou
getData $pathInToDo tte-cd-juarez
getData $pathInDone tte-cd-juarez

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: End of Process. "
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Deleting lock file..."
`rm $lockfile` 

/data/scripts/sendmail.pl -f multisimgen.support@tcl.com -t raffi.semerciyan@jrdcom.com -s "Multisimgen >oplReport.sh< notification" $logfile
